<template>
  <div style="padding-top: 5rem;">
    <img v-if="linkInfo.mini_code" :src="linkInfo.mini_code" alt="" class="code-img">
    <p style="padding: 1rem 2rem; font-size: 14px; color: #000;">{{linkInfo.name}}</p>
    <div v-if="linkInfo.type == 'mini' && fun.getTyep() == 1 && !fun.isTextEmpty(linkInfo.app_source)" :style="{'margin-top': linkInfo.mini_code?'2rem': '7rem'}">
      <!-- 实现打开小程序  -->
      <wx-open-launch-weapp id="launch-btn" class="launch-btn" :username="linkInfo.app_source" :path="linkInfo.link">
        <script type="text/wxtag-template">
          <style>.wx-btn {border:none; width: 8rem;height: 3rem;line-height: 3rem;display: block;margin: 0 auto;border-radius: 1.5rem;color: #fff;text-align: center;background-color: #07c160;}</style>
          <div> <button class="wx-btn">点击打开小程序</button>	</div>
        </script>
      </wx-open-launch-weapp>
      <p style="padding: 1rem; font-size: 12px; color: #999;">点击打开小程序，领取优惠券</p>
      <van-button plain type="info" round color="#999" style="width: 8rem;" @click="goBack">返回</van-button>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      linkInfo: {}
    };
  },
  activated() {
    this.getCPSLink();
  },
  created() {},
  methods: {
    getCPSLink() {
      if (!this.$route.query.activ) return this.$dialog.alert({ message: "缺少重要参数" });
      $http.get("plugin.union-cps.api.shop.generate-link", { activity_id: this.$route.query.activ }, ".").then(response => {
        if (response.result == 1) {
          // h5优先级：小程序 》APP 》h5
          // app优先级：APP 》小程序 》H5
          // 小程序优先级：小程序
          this.linkInfo = response.data;
          if (!this.fun.isCPS() && response.data.type == "mini") {
            // 小程序
            return;
          }
          if (response.data.type == "app") {
            // 跳转app
            this.schemeUrl = response.data.link;
            this.downloadUrl = response.data.link;
            this.jumpApp(this.schemeUrl);
            this.noApp();
            return;
          }
          if (this.fun.isCPS() && response.data.type == "mini") {
            // cpsApp环境下，app调用原生跳转小程序方法
            let json = {
              mini_route: response.data.link,
              mini_id: response.data.app_source
            };
            if (this.fun.isIosOrAndroid() === "ios") {
              miniWXIOS({ body: "miniWXIOS", json: JSON.stringify(json) });
            } else {
              miniWXAndroid(JSON.stringify(json));
            }
            return;
          }
          window.location.href = response.data.link;
        } else {
          this.$dialog.alert({ message: response.msg });
        }
      });
    },
    isBlackApp() {
      var u = navigator.userAgent.toLowerCase();
      return /micromessenger/i.test(u) || u.indexOf("weibo") > -1 || u.indexOf("qq") > -1 || u.indexOf("mqqbrowser") > -1;
    },
    // 跳转打开app
    jumpApp(t) {
      try {
        var e = navigator.userAgent.toLowerCase(),
          n = e.match(/cpu iphone os (.*?) like mac os/);
        if (((n = null !== n ? n[1].replace(/_/g, ".") : 0), parseInt(n) >= 9)) {
          window.location.href = t;
        } else {
          var r = document.createElement("iframe");
          (r.src = t), (r.style.display = "none"), document.body.appendChild(r);
        }
      } catch (e) {
        window.location.href = t;
      }
    },
    // 无响应或者没安装跳转下载
    noApp() {
      var t = Date.now(),
        r = this.downloadUrl;
      this.timer = setTimeout(function() {
        return Date.now() - t > 2200 ? (clearTimeout(this.timer), !1) : !document.webkitHidden && !document.hidden && void (location.href = r);
      }, 2000);
    },
    goBack(){
      if (window.history.length <= 1) {
        this.$router.push(this.fun.getUrl("home", {}));
      } else {
        this.$router.go(-1);
      }
    }
  }
};
</script>

<style lang="scss" rel="stylesheet/scss" scoped>
.code-img {
  width: 12rem;
  height: 12rem;
  display: block;
  margin: 0 auto;
}
</style>
